package cn.wolfcode.consumer._04_consume_type;

import org.apache.rocketmq.client.consumer.DefaultMQPullConsumer;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.PullResult;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.message.MessageQueue;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception {
        //1.创建消费者对象
        //DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("consumer-demo");
        DefaultMQPullConsumer defaultMQPullConsumer = new DefaultMQPullConsumer("consumer-demo");
        //2.设置名字服务器的地址
        //defaultMQPushConsumer.setNamesrvAddr("127.0.0.1:9876");
        defaultMQPullConsumer.setNamesrvAddr("127.0.0.1:9876");
        //3.启动消费者
        defaultMQPullConsumer.start();
        //defaultMQPushConsumer.subscribe("04-consume-type","*");
        //4.拉取消息
        PullResult pullResult = defaultMQPullConsumer.pull(new MessageQueue("04-consume-type", "broker-a", 2), "*", 0, 10);
        List<MessageExt> msgFoundList = pullResult.getMsgFoundList();
        for (MessageExt messageExt : msgFoundList) {
            System.out.println("消息的内容是:"+new String(messageExt.getBody()));
            System.out.println("消息的id是:"+messageExt.getMsgId());
        }
        /*defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("消息的内容是:"+new String(messageExt.getBody()));
                    System.out.println("消息的id是:"+messageExt.getMsgId());
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });*/
        //5.关闭资源
        defaultMQPullConsumer.shutdown();
        //defaultMQPushConsumer.start();
    }
}
